package com.shire.shire_interface.service.friend.impl;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.constants.WxConstants;
import com.shire.shire_interface.dao.friend.WxFriendLogDao;
import com.shire.shire_interface.model.command.WxCommand;
import com.shire.shire_interface.model.friend.WxFriendLog;
import com.shire.shire_interface.model.friend.WxFriendLogExample;
import com.shire.shire_interface.model.user.WxAccount;
import com.shire.shire_interface.service.account.WxAccountService;
import com.shire.shire_interface.service.friend.WxFriendLogService;
import com.shire.shire_interface.service.invite.WxInviteTaskService;
import com.shire.shire_interface.service.marketing.WxGroupTaskService;
import com.shire.shire_interface.service.marketing.WxNewResponseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class WxFriendLogServiceImpl implements WxFriendLogService {

    @Resource
    private WxFriendLogDao friendLogDao;
    @Resource
    private WxGroupTaskService wxGroupTaskService;
    @Resource
    private WxInviteTaskService wxInviteTaskService;
    @Resource
    private WxNewResponseService newResponseService;
    @Resource
    private WxAccountService accountService;

    @Override
    public WxFriendLog getFriendLogByWxId(Integer csId, String wxAccount, String friendWxid) {
        String filteredWxid = friendWxid.replaceAll("[^a-zA-Z0-9]", "");
        WxFriendLogExample example = new WxFriendLogExample();
        example.createCriteria().andWxaccountEqualTo(wxAccount).andFriendwxidEqualTo(friendWxid);
        example.or().andWxaccountEqualTo(wxAccount).andFriendwxidEqualTo(filteredWxid);
        example.setOrderByClause("id desc");
        List<WxFriendLog> list = friendLogDao.selectByExample(example);
        if (list == null || list.size() < 1)
            return null;
        else
            return list.get(0);
    }


    @Override
    public int updateFriendLog(WxAccount account, WxFriendLog friendLog) {
        int ret = friendLogDao.updateByPrimaryKey(friendLog);

        WxConstants.CmdTaskType taskType = null;
        if (friendLog.getTasktype() != null) {
            taskType = WxConstants.CmdTaskType.get(friendLog.getTasktype());
            if (taskType == WxConstants.CmdTaskType.INVITE) {
                wxInviteTaskService.updateForResult(friendLog.getTaskid(), friendLog);
            }

            if (friendLog.getAddresult() == 1 && taskType == WxConstants.CmdTaskType.JOINGROUP)
                wxGroupTaskService.updateForResult(friendLog.getTaskid());
        }

        return ret;
    }

    @Override
    public int inserFriendLog(WxFriendLog friendLog) {
        int ret = friendLogDao.insert(friendLog);

        return ret;
    }

    @Override
    public int updateFriendLogByCmd(WxCommand command) {
        try {
            WxFriendLogExample example = new WxFriendLogExample();
            WxAccount account = accountService.getWxAccountById(command.getAccountid());
            example.createCriteria().andCmdidEqualTo(command.getId());
            JSONObject respContent = JSONObject.parseObject(command.getRespcontent());
            Integer isMyContact = 0;
            if (respContent.containsKey("isMyContact"))
                respContent.getInteger("isMyContact");
            List<WxFriendLog> list = friendLogDao.selectByExample(example);
            WxFriendLog friend = new WxFriendLog();
            if (list != null && list.size() > 0)
                friend = list.get(0);
            if (isMyContact == 1)
                friend.setAddresult(1);
            else
                friend.setAddresult(0);
            friend.setWxaccount(account.getWxid());
            friend.setAddtype(WxConstants.FriendAddType.FROMSEARCH.getCode());
            friend.setFriendnick(respContent.getString("nick"));
            if (respContent.getString("wxId").endsWith("@stranger")) {
                friend.setFriendwxid(respContent.getString("fullPY"));
            }else {
                friend.setFriendwxid(respContent.getString("wxId"));
            }
            friend.setCmdstatus(command.getRststatus());
            friend.setUpdatetime(new Date());
            if (isMyContact == 1){
                return updateFriendLog(account, friend);
            }else
                return friendLogDao.updateByPrimaryKeySelective(friend);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }
}
